package com.tan.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JdbcDemo1 {
    public static void main(String[] args) throws Exception{
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.获取连接
        Connection connection=
                DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mybatis_db?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","root");
        //3.获得预处理对象
        String sql="select * from t_user where user_id=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        //4.给占位符赋值
        preparedStatement.setInt(1,1);
        //5.执行查询
        ResultSet resultSet=preparedStatement.executeQuery();
        //6.遍历查询结果集
        while(resultSet.next()){
            System.out.println(resultSet.getInt("user_id"));
            System.out.println(resultSet.getString("user_name"));
        }
    }
}
